package com.zhiche.lisa.tms.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.tms.dto.ReceiptDTO;
import com.zhiche.lisa.tms.service.AbnormalPodReasonService;
import com.zhiche.lisa.tms.service.PodReasonService;
import com.zhiche.lisa.tms.vo.ReceiptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

/**
 * @author : suohao
 * Date: 2019/7/29
 */
@RestController
@RequestMapping("/podReason")
@Api(value = "/pod", description = "回单接口", tags = {"回单接口"})
public class PodReasonController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PodReasonController.class);

    @Autowired
    private AbnormalPodReasonService abPodReasonService;
    @Autowired
    private PodReasonService podReasonService;


    @GetMapping(value = "/getFirstAbnormalPodReason")
    @ApiOperation(value = "查询异常原因列表(一级)", notes = "查询异常原因列表", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse getFirstAbnormalPodReason() {
        LOGGER.info("PodReasonController.getFirstAbnormalPodReason（查询异常原因列表）");
        RestfulResponse result = new RestfulResponse<>(0, "success", null);
        try {
            result.setData(abPodReasonService.getFirstAbnormalPodReason());
        }  catch (Exception e) {
            LOGGER.error("PodReasonController.getFirstAbnormalPodReason ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getSecondAbnormalPodReason")
    @ApiOperation(value = "根据一级查询异常原因列表(二级)", notes = "查询异常原因列表", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse getSecondAbnormalPodReason(@PathParam("abnormalPodReason") String abnormalPodReason) {
        LOGGER.info("PodReasonController.getSecondAbnormalPodReason（查询异常原因列表）");
        RestfulResponse result = new RestfulResponse<>(0, "success", null);
        try {
            result.setData(abPodReasonService.getSecondAbnormalPodReason(abnormalPodReason));
        }  catch (Exception e) {
            LOGGER.error("PodReasonController.getSecondAbnormalPodReason ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/podReasonToOtm")
    @ApiOperation(value = "回单异常原因推送OTM", notes = "回单异常原因推送OTM", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse podReasonToOtm(@RequestBody ReceiptVO<ReceiptDTO> receiptVO, HttpServletRequest request) {
        LOGGER.info("PodReasonController.podReasonToOtm（回单异常原因推送OTM）");
        RestfulResponse result = new RestfulResponse<>(0, "success", null);
        try {
            abPodReasonService.podReasonToOtm(receiptVO,request);
        }  catch (BaseException ex) {
            LOGGER.error("PodReasonController.podReasonToOtm ERROR... ", ex);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), ex.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PodReasonController.podReasonToOtm ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/getpodReasonList")
    @ApiOperation(value = "查询回单异常原因列表", notes = "查询回单异常原因列表", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<List<Map<String, Object>>> getpodReasonList(@RequestBody Map<String,String> map, @RequestHeader("Authorization") String token) {
        LOGGER.info("PodReasonController.getpodReasonList（查询回单异常原因列表）param：{}",map);
        RestfulResponse<List<Map<String, Object>>> result = new RestfulResponse<>(0, "success", null);
        try {
            result.setData(podReasonService.getpodReasonList(map));
        }  catch (Exception e) {
            LOGGER.error("PodReasonController.getpodReasonList ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/synPodStatus")
    @ApiOperation(value = "同步回单状态", notes = "同步回单状态", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse synPodStatus(@RequestBody String param) {
        LOGGER.info("PodReasonController.synPodStatus（同步回单状态）param：{}",param);
        RestfulResponse result = new RestfulResponse<>(0, "success", null);
        try {
            podReasonService.synPodStatus(param);
        }  catch (Exception e) {
            LOGGER.error("PodReasonController.synPodStatus ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}
